import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class createMaxHeap implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}

public class text {
    //Top-k问题；
    public static int[] SmallK(int[] array,int k){
        int[] ret=new int[k];
        if(array==null||k<=0){
            return ret;
        }
        createMaxHeap createMaxHeap=new createMaxHeap();
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>(createMaxHeap);
        for(int i=0;i<k;i++){
            priorityQueue.offer(array[i]);
        }
        for(int i=k;i< array.length;i++){
            if(array[i]<priorityQueue.peek()){
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
            }
        }
        for(int i=0;i<k;i++){
            ret[i]=priorityQueue.poll();
        }
        return ret;
    }
    public static void main(String[] args){
        int[] array={12,33,4,54,34,9};
        int[] ret=text.SmallK(array,3);
        System.out.println(Arrays.toString(ret));
    }

}
